#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\math\optimization\problem.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Math { namespace Optimization {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IProblem
	public ref class CProblem : 
            public System::MarshalByRefObject,
            public Cephei::QL::Math::Optimization::IProblem
	{
	protected: 
		boost::shared_ptr<QuantLib::Problem>* _ppProblem;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Problem>* _phProblem;
#endif
		Object^ _ProblemOwner;     // reference to object that manages the storage for this object
	internal:
		CProblem (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::Optimization::IConstraint^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ initialValue);
        CProblem (boost::shared_ptr<QuantLib::Problem>& childNative, Object^ owner);
        CProblem (QuantLib::Problem& childNative, Object^ owner);
        CProblem (CProblem^ copy);
        CProblem (System::Type^ t);
#ifdef STRUCT
        CProblem (QuantLib::Problem childNative);
#endif       
#ifdef HANDLE
		CProblem (QuantLib::Handle<QuantLib::Problem>& childNative, Object^ owner);
		CProblem (QuantLib::Handle<QuantLib::Problem> childNative);
#endif
		virtual ~CProblem ();
		!CProblem ();

	internal:
		QuantLib::Problem& GetReference ();
		boost::shared_ptr<QuantLib::Problem>& GetShared ();
		QuantLib::Problem* GetPointer ();
        void SetProblem (boost::shared_ptr<QuantLib::Problem> native)
        {
            if (_ppProblem != NULL)
                delete _ppProblem;
            _ppProblem = new boost::shared_ptr<QuantLib::Problem> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Problem>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Cephei::QL::Math::Optimization::IConstraint^ Constraint 
        {
		    virtual Cephei::QL::Math::Optimization::IConstraint^ get () ;
        }
        property Cephei::QL::Math::Optimization::ICostFunction^ CostFunction 
        {
		    virtual Cephei::QL::Math::Optimization::ICostFunction^ get () ;
        }
        property Cephei::QL::Math::IArray^ CurrentValue 
        {
		    virtual Cephei::QL::Math::IArray^ get () ;
        }
        property Int32 FunctionEvaluation 
        {
		    virtual Int32 get () ;
        }
        property Double FunctionValue 
        {
		    virtual Double get () ;
        }
		virtual Cephei::QL::Math::Optimization::IProblem^ Gradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x) ;
        property Int32 GradientEvaluation 
        {
		    virtual Int32 get () ;
        }
        property Double GradientNormValue 
        {
		    virtual Double get () ;
        }
        property Cephei::QL::Math::Optimization::IProblem^ Reset 
        {
		    virtual Cephei::QL::Math::Optimization::IProblem^ get () ;
        }
		virtual Cephei::QL::Math::Optimization::IProblem^ SetCurrentValue (Cephei::QL::Math::IArray^ currentValue) ;
		virtual Cephei::QL::Math::Optimization::IProblem^ SetFunctionValue (Double functionValue) ;
		virtual Cephei::QL::Math::Optimization::IProblem^ SetGradientNormValue (Double squaredNorm) ;
		virtual Double Value (Cephei::QL::Math::IArray^ x) ;
		virtual Double ValueAndGradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CProblem_Factory : public System::MarshalByRefObject,  public IProblem_Factory
	{
	public:
        virtual IProblem^ Create (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::Optimization::IConstraint^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ initialValue);
    };
   
/*Cephei*/ } /*QL*/ } /*Math*/ } /*Optimization */}
